package worldutils.huntgame;

import org.bukkit.*;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import worldutils.huntgame.AutoStart.Timer;
import worldutils.huntgame.Commands.mainCommand;
import worldutils.huntgame.Listener.AirDropSet;
import worldutils.huntgame.Listener.mainListener;
import worldutils.huntgame.Utils.CompassUpdater;
import worldutils.huntgame.Utils.RandomSpawnUtils;
import worldutils.huntgame.Utils.TimerUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

public final class HuntGame extends JavaPlugin {
    public static String author = "_NEVERL0SE_";
    public static  String version = "release(1.2)";
    public static Boolean Gamestatus = false;
    public static Boolean DevMode = true;
    public static Boolean HunterCantMove = false;
    public static Location TaoshengSpawn = null;
    public static Location ZhuishaSpawn = null;
    private static HuntGame instance;
    public static int warnrange;
    public static int runTime;
    public static BukkitRunnable compassNotificationTask;
    public static Boolean AutoGetSpawn;
    public static Boolean endgameonenter;
    public static int starttime;
    public static int minautostartplayers;
    private static YamlConfiguration messageConfig;
    private File messageFile;

    public static String AirDropChestNR;

    public static int AirDropR;


    @Override
    public void onEnable() {
        // Plugin startup logic
        createConfigFiles();
        loadMessageConfig();
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + " ____ ____ _  _ ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "(_  _|_   | \\/ )");
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "  )(  / /_ )  ( ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + " (__)(____|_/\\_)");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "      ___          ___          ___                 ___          ___          ___          ___    ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "     /  /\\        /  /\\        /  /\\    ___        /  /\\        /  /\\        /  /\\        /  /\\   ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "    /  /:/       /  /:/       /  /::|  /__/\\      /  /::\\      /  /::\\      /  /::|      /  /::\\ ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "   /  /:/       /  /:/       /  /:|:|  \\  \\:\\    /  /:/\\:\\    /  /:/\\:\\    /  /:|:|     /  /:/\\:\\ ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "  /  /::\\ ___  /  /:/       /  /:/|:|__ \\__\\:\\  /  /:/  \\:\\  /  /::\\ \\:\\  /  /:/|:|__  /  /::\\ \\:\\ ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + " /__/:/\\:\\  /\\/__/:/     /\\/__/:/ |:| /\\/  /::\\/__/:/_\\_ \\:\\/__/:/\\:\\_\\:\\/__/:/_|::::\\/__/:/\\:\\ \\:\\");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + " \\__\\/  \\:\\/:/\\  \\:\\    /:/\\__\\/  |:|/:/  /:/\\:\\  \\:\\__/\\_\\/\\__\\/  \\:\\/:/\\__\\/  /~~/:/\\  \\:\\ \\:\\_\\/");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "      \\__\\::/  \\  \\:\\  /:/     |  |:/:/  /:/__\\/\\  \\:\\ \\:\\       \\__\\::/       /  /:/  \\  \\:\\ \\:\\  ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "      /  /:/    \\  \\:\\/:/      |__|::/__/:/      \\  \\:\\/:/       /  /:/       /  /:/    \\  \\:\\_\\/ ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "     /__/:/      \\  \\::/       /__/:/\\__\\/        \\  \\::/       /__/:/       /__/:/      \\  \\:\\   ");
        Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "     \\__\\/        \\__\\/        \\__\\/               \\__\\/        \\__\\/        \\__\\/        \\__\\/  ");

        runTime = getConfig().getInt("runtime");
        AutoGetSpawn = getConfig().getBoolean("auto-spawn");
        endgameonenter = getConfig().getBoolean("end-game-on-enter");
        starttime = getConfig().getInt("start-time");
        minautostartplayers = getConfig().getInt("min-players");
        warnrange = getConfig().getInt("warn-range");
        AirDropR = getConfig().getInt("air-drop-block");
        AirDropChestNR = getConfig().getString("air-drop-chest");


        Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "==================================================");
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "作者:" + author);
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "插件:猎人游戏/HuntGame");
        Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "版本:" + version);
        Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "==================================================");


        Bukkit.getPluginManager().registerEvents(new mainListener(),this);
        Bukkit.getPluginManager().registerEvents(new AirDropSet(),this);


        this.getCommand("hg").setExecutor(new mainCommand(this));
        CompassUpdater.initialize(this);
        Timer.initialize(this);
        TimerUtils.initialize(this);

//        deleteWorldFolder("world");
//        deleteWorldFolder("world_nether");
//        deleteWorldFolder("world_the_end");
//
//        // 生成新世界
//        regenerateWorld("world", World.Environment.NORMAL);       // 主世界
//        regenerateWorld("world_nether", World.Environment.NETHER); // 地狱
//        regenerateWorld("world_the_end", World.Environment.THE_END); // 末地



        if (AutoGetSpawn){
            HuntGame.TaoshengSpawn = RandomSpawnUtils.getRandomLocation(Bukkit.getWorld("world"), 0,0);
            HuntGame.ZhuishaSpawn = RandomSpawnUtils.getRandomLocationTwo(HuntGame.TaoshengSpawn.getWorld(),HuntGame.TaoshengSpawn);
        }
    }

    private void createConfigFiles() {
        // 创建并保存config.yml
        saveDefaultConfig();

        // 创建message.yml
        File messageFile = new File(getDataFolder(), "message.yml");
        if (!messageFile.exists()) {
            saveResource("message.yml", false);
        }
    }

    private void loadMessageConfig() {
        File messageFile = new File(getDataFolder(), "message.yml");
        messageConfig = YamlConfiguration.loadConfiguration(messageFile);
    }

    public static String getMessage(String path) {
        return messageConfig.getString(path).replace("&","§");
    }


    public void saveMessageConfig() {
        try {
            messageConfig.save(new File(getDataFolder(), "message.yml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void reloadConfigFiles() {
        // 重新载入config.yml
        reloadConfig();

        // 重新载入message.yml
        if (messageFile == null) {
            messageFile = new File(getDataFolder(), "message.yml");
        }
        messageConfig = YamlConfiguration.loadConfiguration(messageFile);
    }

    private void regenerateWorld(String worldName, World.Environment environment) {
        // 随机生成种子
        long seed = new Random().nextLong();

        // 创建新世界
        WorldCreator wc = new WorldCreator(worldName);
        wc.environment(environment);
        wc.seed(seed);
        wc.type(WorldType.NORMAL);
        World newWorld = Bukkit.createWorld(wc);

        // 输出世界生成信息
        getLogger().info("生成了一个新的世界 [" + worldName + "]，种子：" + seed);
    }

    private void deleteWorldFolder(String worldName) {
        Path worldFolder = Paths.get(Bukkit.getWorldContainer().getPath(), worldName);
        try {
            if (Files.exists(worldFolder)) {
                Files.walk(worldFolder)
                        .map(Path::toFile)
                        .forEach(File::delete);

                Files.deleteIfExists(worldFolder);
                getLogger().info("删除旧世界 [" + worldName + "] 成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
